package com.cd2cd.code_app;

import javafx.concurrent.Worker;
import javafx.scene.control.Tab;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import netscape.javascript.JSObject;
import org.w3c.dom.NodeList;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.html.HTMLAnchorElement;

public class MyTab extends Tab {

    private WebEngine WEB_ENGINE;
    private static final String BLANK = "_blank";
    private static final String TARGET = "target";
    private static final String CLICK = "click";
    private String url;

    public MyTab(String url) {
        this.url = url;
        final WebView browser = new WebView();

        WEB_ENGINE = browser.getEngine();

        // 使用webview全屏占满
        StackPane root = new StackPane();
        root.getChildren().add(browser);

        WEB_ENGINE.getLoadWorker().stateProperty()
                .addListener((ov, oldState, newState) -> {
                    if (newState == Worker.State.SUCCEEDED) {
                        JSObject win = (JSObject) WEB_ENGINE.executeScript("window");
                        win.setMember("DispatcherHandle", App.DISPATCHER_HANDLE);// 设置变量
                    }
                });

        WEB_ENGINE.documentProperty().addListener((observable, ov, document)->{
            if (document != null) {
                NodeList nodeList = document.getElementsByTagName("a");
                for (int i = 0; i < nodeList.getLength(); i++) {
                    org.w3c.dom.Node node = nodeList.item(i);
                    org.w3c.dom.Node targetNode = node.getAttributes().getNamedItem(TARGET);
                    if (targetNode != null) {
                        String target = targetNode.getTextContent();
                        if (BLANK.equals(target)) {//页面中target="_blank"的在新标签中打开
                            EventTarget eventTarget = (EventTarget) node;
                            eventTarget.addEventListener(CLICK, (evt) -> {
                                HTMLAnchorElement anchorElement = (HTMLAnchorElement) evt.getCurrentTarget();
                                String href = anchorElement.getHref();
                                System.out.println(href);
                                MyTab tab = new MyTab(href);
                                tab.setText("加载中...");
                                App.TAB_PANE.getTabs().add(tab);
                                evt.preventDefault();
                            }, false);
                        }
                    }
                }
            }
        });

        // 标题
        WEB_ENGINE.titleProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null && !newValue.trim().isEmpty()) {
                setText(newValue);
                setTooltip(new Tooltip(newValue));
            }
        });

        WEB_ENGINE.setOnAlert(event -> System.out.println("setOnAlert..."+event.getData()));

        WEB_ENGINE.load(url);

        setContent(browser);
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
